<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
</head>
<body>
<input type="text" id="i1" placeholder="请输入数字"><br>
<input type="text" id="i2" placeholder="请输入数字"><br>
<button onclick="add()">加</button>
<button onclick="subtract()">减</button>
<button onclick="multiply()">乘</button>
<button onclick="division()">除</button>
<h3>运算结果：<span class="result"></span></h3>

<script>
    var num1,num2;
    function getValue(){
        let i1 = document.getElementById('i1');
        let i2 = document.getElementById('i2');
        num1 = parseFloat(i1.value);
        num2 = parseFloat(i2.value);
    }
    function add(){
        getValue();

        r = num1+num2;
        displayInSpan(r);
    }
    function subtract(){
        getValue();
        r = num1-num2;
        displayInSpan(r);
    }
    function multiply(){
        getValue();
        r = num1*num2;
        displayInSpan(r);
    }
    function division(){
        getValue();
        if(num2===0){
            alert('除数不能为0')
        }else{
            r = num1/num2;
        }
        displayInSpan(r);
    }
    function displayInSpan(x){
        let span = document.querySelector('.result');
        span.innerHTML= x;
    }
</script>
</body>
</html>